<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

/**
 * 微信查看申请
 */
class WechatCheckApply extends Model
{
    protected $table = 'wechat_check_apply';

    protected $fillable = [
        'user_id', 'status', 'host_id'
    ];

    const APPLY_ING = 0;
    const APPLY_PASS = 1;
    const APPLY_REFUSE = 2;
    const APPLY_EXP = 3;

    static function addLog($userId = 0, $hostId = 0)
    {
        $where = [
            'user_id' => $userId,
            'host_id' => $hostId,
        ];
        return self::updateOrCreate($where, [
            'status' => self::APPLY_ING
        ]);
    }

    /**
     * 查询记录
     */
    static function checkLog($userId = 0, $hostId = 0, $status = -1)
    {
        $where = [
            'user_id' => $userId,
            'host_id' => $hostId,
        ];
        $status > -1 && $where['status'] = $status;
        $item = self::where($where)->orderBy('id', 'desc')->first();
        return $item ?: null;
    }

    static function updateResult($logId = 0, $op = 1)
    {
        $status = $op == 1 ? self::APPLY_PASS : self::APPLY_REFUSE;
        return self::where('id', $logId)->limit(1)->update(['status' => $status]);
    }

    static function expireLog($logId = 0)
    {
        return self::where('id', $logId)->limit(1)->update(['status' => self::APPLY_EXP]);
    }
}
